#!/usr/bin/env python
#-*-coding:utf-8-*-

# pid_file_oss.py is part of "lemon-py" (http://code.google.com/p/lemon-py/).
# Copyright (C) 2010  - JBC <JBC dot Develop at gmail dot com >

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


################################################################################
# DOCS
################################################################################

"""Single innstance class and utilities for pid proc os (like posix and osx)"""


################################################################################
# META
################################################################################

__version__ = "0.1"
__license__ = "GPL3"
__author__ = "JBC"
__since__ = "0.1"
__date__ = "2010-06-23"


################################################################################
# IMPORTS
################################################################################

import subprocess
import shlex
import os

import core


################################################################################
# CLASS
################################################################################

class SingleInstance(core.SingleInstanceBase):

    def setup(self, **kwargs):
        """Setup the app for only one instance run

            @param pidpath: The path to the pid file

        """
        self.pidpath = kwargs["pidpath"]
        self.lasterror = False
        if os.path.isfile(self.pidpath):
            fp = open(self.pidpath, "r")
            pid = fp.read().strip()
            fp.close()
            if pid:
                proc = subprocess.Popen(shlex.split(r"ls /proc"),
                                        stdout=subprocess.PIPE,
                                        stdin=subprocess.PIPE)
                proc.wait()
                for l in proc.stdout.read().splitlines():
                    if l and l.strip() == pid:
                        self.lasterror = True
                        break
        if not self.lasterror:
            fp = open(self.pidpath, "w")
            fp.write(str(os.getpid()))
            fp.close()

    def isrunning(self):
        return self.lasterror

    def __del__(self):
        if not getattr(self, "lasterror", None) \
           and getattr(self, "pidpath", None):
            os.unlink(self.pidpath)
            
            
################################################################################
# MAIN
################################################################################

if __name__ == "__main__":
    print __doc__
